package com.atguigu.springcloud.service.impl;

import com.atguigu.springcloud.dao.OrderDao;
import com.atguigu.springcloud.pojo.Order;
import com.atguigu.springcloud.service.AccountService;
import com.atguigu.springcloud.service.OrderService;
import com.atguigu.springcloud.service.StorageService;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @Author: robert.song
 * @Date: Created in 2021/12/15
 */
@Slf4j
@Service
public class OrderServiceImpl implements OrderService {
    @Resource
    private OrderDao orderDao;
    @Resource
    private StorageService storageService;
    @Resource
    private AccountService accountService;

    @Override
    @GlobalTransactional(name = "fsp_create_order",rollbackFor = Exception.class)
    public int create(Order order) {
        log.info("新建订单");
        orderDao.create(order);
        log.info("调用库存，扣count");
        storageService.decrease(order.getProductId(),order.getCount());
        log.info("count扣减完成");
        log.info("调用账户，扣money");
        accountService.decrease(order.getUserId(),order.getMoney());
        log.info("money扣除完成");
//        修改订单状态，从0到1，1代表已经完成
        log.info("修改订单状态开始");
        orderDao.update(order.getUserId(),0);
        log.info("修改订单状态结束");
        log.info("下单结束");
        return 1;
    }

}
